package org.raymond.iworks.study.mq.rabbitmq.classic.alternate;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class NormalTask {
    public static void main(String[] args) throws Exception {
        String message = "to elx..";
        try(Connection connection = DLXWorker.connection();
            Channel channel = connection.createChannel()){
            Map<String, Object> map = new HashMap<>();
            map.put("x-dead-letter-exchange", DLXWorker.DLX_EXCHANGE_NAME);
            map.put("x-dead-letter-routing-key", DLXWorker.DLX_ROUTING_NAME);
            map.put("x-message-ttl", 10*1000);
            channel.queueDeclare(NormalWorker.QUEUE_NAME, true, false, false, map);

            boolean durable = false;
            boolean autoDelete = false;
            Map<String, Object> withAEMap = new HashMap<>();
            withAEMap.put("alternate-exchange", AEWorker.ALTERNATE_EXCHANGE_NAME);
            channel.exchangeDeclare(NormalWorker.EXCHANGE_NAME, BuiltinExchangeType.TOPIC, durable, autoDelete, withAEMap);

            for(int i=0; i<1; i++){
                // exchange不存在时,生产者会报错, reply-text=NOT_FOUND
                // exchange为空时,路由到了/cloud下的default
                // routingkey未绑定到queue或为空时,会路由到AE上去
                channel.basicPublish(NormalWorker.EXCHANGE_NAME, NormalWorker.ROUTING_KEY, null, message.getBytes(StandardCharsets.UTF_8));
            }
            log.info("[x] sent: {}:{}", "", message);
        }
    }


}
